package com.hetao.work;


import com.alibaba.fastjson.JSON;
import com.hetao.work.model.HttpResult;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Optional;

/**
 * @author hetao
 */
public class HttpClient {

    public HttpResult get(String url) {
        HttpResult result = new HttpResult();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                result.setCode(response.getStatusLine().getStatusCode());
                result.setContent(EntityUtils.toString(responseEntity));
                return result;
            }
        } catch (ParseException | IOException e) {
            result.setErrorMsg(e.getMessage());
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                result.setErrorMsg(e.getMessage());
            }
        }
        result.setCode(-1);
        return result;
    }

    public HttpResult post(String url, Object body) {
        HttpResult result = new HttpResult();
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);

        Optional<Object> optionalBody = Optional.ofNullable(body);
        if (optionalBody.isPresent()) {
            StringEntity entity = new StringEntity(JSON.toJSONString(body), "UTF-8");
            httpPost.setHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
            httpPost.setEntity(entity);
        }

        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                result.setCode(response.getStatusLine().getStatusCode());
                result.setContent(EntityUtils.toString(responseEntity));
                return result;
            }
        } catch (ParseException | IOException e) {
            result.setErrorMsg(e.getMessage());
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                result.setErrorMsg(e.getMessage());
            }
        }
        result.setCode(-1);
        return result;
    }

}
